package org.biubiu0002.baiblog.core.task;

import lombok.extern.slf4j.Slf4j;
import org.biubiu0002.baiblog.core.service.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author biubiu0002
 * Redis定时写数据回数据库
 */
@Component
@Slf4j
public class RedisTask {

    @Resource
    private LikeRecordService likeRecordService;

    @Resource
    private ShareLikeCountService shareLikeCountService;

    @Resource
    private ShareCommentCountService shareCommentCountService;

    @Resource
    private ShareFavorCountService shareFavorCountService;

    /**
     * 每隔2小时把点赞记录写入数据库
     */
    @Scheduled(cron = "0 0 */2 * * ?")
    private void syncShareLikeMapFromRedis2DB(){
        log.info("======每隔2小时把点赞记录写入数据库======");
        likeRecordService.syncShareLikeMapFromRedis2DB();
    }

    /**
     * 每隔2小时把推文点赞数写入数据库
     */
    @Scheduled(cron = "0 0 */2 * * ?")
    private void syncShareLikeCountFromRedis2DB(){
        log.info("======每隔2小时把推文点赞数写入数据库======");
        shareLikeCountService.syncShareLikeCountFromRedis2DB();
    }

    /**
     * 每隔2小时把推文评论数写入数据库
     */
    @Scheduled(cron = "15 0 */2 * * ?")
    private void syncShareCommentCountFromRedis2DB(){
        log.info("======每隔2小时把推文评论数写入数据库======");
        shareCommentCountService.syncShareCommentCountFromRedis2DB();
    }

    /**
     * 每隔2小时把推文收藏数写入数据库
     */
    @Scheduled(cron = "30 0 */2 * * ?")
    private void syncShareFavorCountFromRedis2DB(){
        log.info("======每隔2小时把推文收藏数写入数据库======");
        shareFavorCountService.syncShareFavorCountFromRedis2DB();
    }

    /**
     * 每周一凌晨4点30删除每周点赞排行榜key
     */
    @Scheduled(cron = "0 30 4 ? * MON")
    private void deleteWeeklyLikeCountBoard(){
        log.info("======每周一凌晨4点删除每周点赞排行榜key======");
        shareLikeCountService.deleteWeeklyLikeCountBoard();
    }

}
